<!-- 门店列表 -->
<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAddStore"> 添加门店</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <!--抽屉-->
    <StoreDrawer @register="registerDrawer" @success="handleSuccess"/>
  </div>
</template>

<script lang="ts" name="shop-store" setup>
//ts语法
import {ActionItem, BasicTable, TableAction} from '/@/components/Table'
import StoreDrawer from './StoreDrawer.vue';
import {Icon} from '/@/components/Icon';
import {useDrawer} from '/@/components/Drawer';
import {useListPage} from '/@/hooks/system/useListPage'
import {useMessage} from '/@/hooks/web/useMessage';
import {columns, searchFormSchema} from './store.data';
import {batchDeleteStore, deleteStore, pageStore} from './store.api';
// import { usePermission } from '/@/hooks/web/usePermission'
// const { hasPermission } = usePermission();

const { createMessage, createConfirm } = useMessage();

//注册drawer
const [registerDrawer, { openDrawer }] = useDrawer();

// 列表页面公共参数、方法
const { prefixCls, tableContext } = useListPage({
  designScope: 'store-list',
  tableProps: {
    title: '门店列表',
    api: pageStore,
    bordered: false,
    columns: columns,
    size: 'small',
    formConfig: {
      showResetButton: false,
      schemas: searchFormSchema,
    },
    actionColumn: {
      width: 120,
    },
    beforeFetch: (params) => {
      return Object.assign({ 'column': 'createdAt', 'order': "desc" }, params)
    },
  }
})


//注册table数据
const [registerTable, { reload, updateTableDataRecord }, { rowSelection, selectedRowKeys }] = tableContext

/**
 * 添加
 */
function handleAddStore() {
  openDrawer(true, {
    isTenantMember: false
  })
}


/**
 * 编辑事件
 */
async function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
    showFooter: true,
  });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteStore({ id: record.id }, reload);
}
/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDeleteStore({ ids: selectedRowKeys.value }, () => {
    selectedRowKeys.value = []
    reload()
  })
}
/**
 * 成功回调
 */
function handleSuccess({ values }) {
  reload()
  updateTableDataRecord(values.id, values);
  createMessage.success("编辑成功")
}

/**
 * 操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record)
    },
    {
      label: '删除',
      ifShow: () => !isMainTenant(record),
      popConfirm: {
        title: `是否确认删除 ${record.name}`,
        confirm: handleDelete.bind(null, record)
      },
    },
  ]
}

const isMainTenant = (record) => {
  return record.tenantId === record.mainTenantId
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    
  ]
}
</script>

<style scoped></style>
